/*
 * lr-wpan-associated-device.cc
 *
 *  Created on: 18 févr. 2012
 *      Author: Stéphane Kamga < stepos01@gmail.com>
 */

#include "ns3/log.h"
#include "ns3/lr-wpan-associated-device.h"



NS_LOG_COMPONENT_DEFINE ("LrWpanAssociatedDevice");

namespace ns3 {

NS_OBJECT_ENSURE_REGISTERED (LrWpanAssociatedDevice);


TypeId
LrWpanAssociatedDevice::GetTypeId(void)
{
  static TypeId tid = TypeId ("ns3::LrWpanAssociatedDevice")
    .SetParent<Object> ()
    .AddConstructor<LrWpanAssociatedDevice>();


  return tid;
}

LrWpanAssociatedDevice::LrWpanAssociatedDevice(void)
{

}

LrWpanAssociatedDevice::~LrWpanAssociatedDevice(void)
{

}

 void
 LrWpanAssociatedDevice::addAssociateDevice(Mac16Address shortaddr,Mac64Address extaddr)
{
	 m_associateDevice.push_back(associateDeviceAddress(shortaddr,extaddr));
}



 bool
 LrWpanAssociatedDevice::searchAssociateDevice(Address add, LrWpanAddressMode addrMode)
{
	 NS_ASSERT_MSG(((addrMode==2)||(addrMode==3)),"invalid LrWpanAddressMode parameter");
	 bool found = false;
	 if(m_associateDevice.empty())
	 {
		 NS_LOG_ERROR("coordinator has no associated device yet");
		 return found;
	 }

	 std::list<associateDeviceAddress>::iterator it;
	 if(addrMode == SHORT_ADDR)
	 {
		 for(it = m_associateDevice.begin();it != m_associateDevice.end();it ++)
		 {
			 if(it->first == Mac16Address::ConvertFrom(add))
			 {
				 found = true;
				 break;
			 }
		 }
	 }
	 else
	 {
		 for(it = m_associateDevice.begin();it != m_associateDevice.end();it ++)
		 {
			 if(it->second == Mac64Address::ConvertFrom(add))
			 {
				 found = true;
				 break;
			 }
		 }
	 }

	 return found;
}

 Address
  LrWpanAssociatedDevice::getAssociateDevice(Address add, LrWpanAddressMode addrMode)
 {
 	 NS_ASSERT_MSG(((addrMode==2)||(addrMode==3)),"invalid LrWpanAddressMode parameter");
 	  	 if(m_associateDevice.empty())
 	 {
 		 NS_LOG_ERROR("coordinator has no associated device yet");
 		 return Address();
 	 }
 	  	 Address returnAddress;
 	 std::list<associateDeviceAddress>::iterator it;
 	 if(addrMode == SHORT_ADDR)
 	 {
 		 for(it = m_associateDevice.begin();it != m_associateDevice.end();it ++)
 		 {
 			 if(it->first == Mac16Address::ConvertFrom(add))
 			 {
 				 returnAddress = it->second.operator ns3::Address();
 				 break;
 			 }
 		 }
 	 }
 	 else
 	 {
 		 for(it = m_associateDevice.begin();it != m_associateDevice.end();it ++)
 		 {
 			 if(it->second == Mac64Address::ConvertFrom(add))
 			 {
 				 returnAddress = it->first.operator ns3::Address();
 				 break;
 			 }
 		 }
 	 }
 	return returnAddress;
 }

void
LrWpanAssociatedDevice::removeAssociateDevice(Address add, LrWpanAddressMode addrMode)
{

	 NS_ASSERT_MSG(((addrMode==2)||(addrMode==3)),"invalid LrWpanAddressMode parameter");
	 if(m_associateDevice.empty())
	 {
		 NS_LOG_ERROR("coordinator has no associated device yet");
		 return ;
	 }
	 bool found =  false;
	 std::list<associateDeviceAddress>::iterator it;
	 if(addrMode == SHORT_ADDR)
	 {
		 for(it = m_associateDevice.begin();it != m_associateDevice.end();it ++)
		 {
			 if(it->first == Mac16Address::ConvertFrom(add))
			 {
				 found = true;
				 m_associateDevice.erase(it);
				 break;
			 }
		 }
	 }
	 else
	 {
		 for(it = m_associateDevice.begin();it != m_associateDevice.end();it ++)
		 {
			 if(it->second == Mac64Address::ConvertFrom(add))
			 {
				 found = true;
				 m_associateDevice.erase(it);
				 break;
			 }
		 }
	 }
	 if(!found)
	 {
		 NS_LOG_ERROR("No device with the given address found");
	 }
}
void
LrWpanAssociatedDevice::setMac(Ptr<LrWpanMac> mac)
{
	m_mac = mac;
}

void
LrWpanAssociatedDevice::DoDispose()
{
	m_mac = 0;
/*	 std::list<associateDeviceAddress>::iterator it;
	for(it = m_associateDevice.begin();it != m_associateDevice.end();it ++)
	{
		m_associateDevice.erase(it);
	}
*/
	m_associateDevice.clear();
}

}  // namespace ns3


